/*   Copyright 2012 Fernando Moreno Ruiz All Rights Reserved.
 *
 * 	 This file is part of pyPhotosMixer.
 *
 *   pyPhotosMixer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   pyPhotosMixer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with pyPhotosMixer.  If not, see <http://www.gnu.org/licenses/>.
 * */
import QtQuick 1.1
import "../javascript/Date.js" as JsDate
import "../date"

/*
    This class generate all information for each file on the repeater model
*/
Repeater {
    id:repeaterCamera
    property variant listToGenerate; //list generated with information of repeater model
    property string maxDate:"";
    property string minDate:"";
    property string maxDateFerFormat:"";
    property string minDateFerFormat:"";
    signal loadFinished()
    property bool debug:false
    function gDebug(str){
        if(debug)
            console.log("GenerateModelCamera:" + str);
    }
    delegate:Item{
        FerFormat{
            id:ferFormat
            file: fileName
        }
        Component.onCompleted: {

            gDebug(index);
            //get exif date of file
            var exifDate = exif.getDatePicture(filePath);
            var thumbnailFilePath
            //create thumbail of photo file
            thumbnailFilePath = exif.createThumbail(filePath)

            //if no error
            if(exifDate != "error"){

                //if maxDate is not set yet
                if(maxDate==""){

                    //first date is maxDate
                    maxDate = exifDate

                }else{

                    //if maxDate is set yet, we compare maxDate
                    if(JsDate.stringToDate(exifDate)>JsDate.stringToDate(maxDate)){

                        //if maxDate is lower, we change date
                        maxDate = exifDate

                    }

                }

                //if minDate is not set yet
                if(minDate==""){

                    //first date is minDate
                    minDate = exifDate

                }else{

                    //if minDate is set yet, we compare minDate
                    if(JsDate.stringToDate(exifDate)<JsDate.stringToDate(minDate)){

                        //if minDate is higher, we change date
                        minDate = exifDate

                    }
                }
            }


            var strFile = filePath+""
            gDebug(typeof strFile)
            gDebug(strFile)
            // we append file to list
            if(typeof strFile==="string"){
                gDebug("FILE:")
                gDebug("extension"+strFile.substring(strFile.length-4),4)

            }
            var i = ""

            if(strFile.length>4 && strFile.substring(strFile.length-4).toUpperCase()===".JPG"){
                gDebug("APPEND:"+strFile)
                listToGenerate.append({"file":strFile,"thumbail":thumbnailFilePath,"fileName":fileName,"indexPicture":index,"exif":exifDate})
            }


            if(maxDateFerFormat==""){
                maxDateFerFormat = ferFormat.dateShow
            }else{
                if(ferFormat.dateShow!=""){
                    if(JsDate.stringToDate(ferFormat.dateShow)>JsDate.stringToDate(maxDateFerFormat)){
                        maxDateFerFormat = ferFormat.dateShow
                    }
                }
            }
            if(minDateFerFormat==""){
                minDateFerFormat = ferFormat.dateShow
            }else{
                if((ferFormat.dateShow!="") && JsDate.stringToDate(ferFormat.dateShow)<JsDate.stringToDate(minDateFerFormat)){
                    minDateFerFormat = ferFormat.dateShow
                }
            }
            if(index==repeaterCamera.count-1){
                gDebug("Load Finished!"+ filePath)
                loadFinished()
            }
        }
    }
}
